package cn.com.b2office.struts2.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.Cart;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.CartService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * add choosed item to member's shopping cart
 * 
 * @author wangtao.upc@gmail.com
 * 
 */
public class AddCartItemAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -5624748836499187789L;

	private CartService cartService = null;

	private String itemId = null;

	private int qty = 1;

	/** 默认中个 */
	private int shape = 2;

	private String comments = null;

	private Map session = null;

	public String execute() throws Exception {
		Cart cart = (Cart) session.get("cart");
		User user = (User) session.get("user");
		if (cart == null) {
			if (user != null) {
				cart = cartService.createCart(user.getMemberId());
			} else {
				cart = cartService.createAnonymousCart();
			}
			session.put("cart", cart);
		}
		cartService.addItemToCart(cart, itemId, qty, shape, comments);
		// System.out.println(cart.getCartSize()+": "+cart.getSum()+">>>>>>>
		// "+shape+" qty = "+qty);
		return SUCCESS;

	}

	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public int getShape() {
		return shape;
	}

	public void setShape(int shape) {
		this.shape = shape;
	}

}
